/**
 * Copyright 2006 gworks.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 *  The greatest achievement is selflessness. The greatest worth is self-mastery.
 *  The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 *  The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 *  The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 *  The greatest goodness is a peaceful mind. The greatest patience is humility.
 *  The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 *  The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 * 
 * @author Ashin Wimalajeewa (ash)
 */
package org.javaongems.rico.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;

public class SizeAndPositionEffect extends ElementEffect {
	final static public int OPTION_MOVE = 1;
	final static public int OPTION_SIZE = 1 << 1;
	
	final static public int OPTIONS_MOVESIZE = OPTION_MOVE | OPTION_SIZE;

	private int options;
	private int[] orig = new int[4], targ = new int[4];

	public SizeAndPositionEffect(Element elem, int x, int y, int w, int h, int o) {
		super(elem);
		targ[0] = x;
		targ[1] = y;
		targ[2] = w;
		targ[3] = h;
		options = o;
	}
	
	public void reverse() {
		super.reverse();
		for (int i = 0; i < orig.length; i++) 
			targ[i] = orig[i];
		getCurrentSizePosition(orig);
	}
	
	public void start() {
		super.start();
		getCurrentSizePosition(orig);
	}

	public void transition(int step, int remainingSteps, int stepDuration) {
		super.transition(step, remainingSteps, stepDuration);
		boolean isMoving = ((options & OPTION_MOVE) != 0);
		boolean isSizing = ((options & OPTION_SIZE) != 0);
		int[] current = new int[4];
		getCurrentSizePosition(current);
		
		if (isMoving) {
			int difX = (targ[0] - current[0]) / remainingSteps;
			int difY = (targ[1] - current[1]) / remainingSteps;
			moveBy(difX, difY, current[0], current[1]);
		}
		if (isSizing) {
			int difW = (targ[2] - current[2]) / remainingSteps;
			int difH = (targ[3] - current[3]) / remainingSteps;
			resizeBy(difW, difH, current[2], current[3]);
		}
	}
	
	protected void getCurrentSizePosition(int[] store) {
		store[0] = getElementOffsetLeft(element);
		store[1] = getElementOffsetTop(element);
		store[2] = getElementOffsetWidth(element);
		store[3] = getElementOffsetHeight(element);
	}

	protected void moveBy(int deltaX, int deltaY, int currX, int currY) {
		DOM.setStyleAttribute(element, "left", (currX + deltaX) + "px");
		DOM.setStyleAttribute(element, "top", (currY + deltaY) + "px");
	}

	protected void resizeBy(int deltaX, int deltaY, int currW, int currH) {
		DOM.setStyleAttribute(element, "width", (currW + deltaX) + "px");
		DOM.setStyleAttribute(element, "height", (currH + deltaY) + "px");
	}
	
	native static private int getElementOffsetLeft(Element e) /*-{
		return e.offsetLeft;
	}-*/;
	
	native static private int getElementOffsetTop(Element e) /*-{
		return e.offsetTop;
	}-*/;
	
	native static private int getElementOffsetWidth(Element e) /*-{
		return e.offsetWidth;
	}-*/;
	
	native static private int getElementOffsetHeight(Element e) /*-{
		return e.offsetHeight;
	}-*/;
}
